package com.example.es.controller;

import java.util.concurrent.*;
import java.util.concurrent.atomic.AtomicInteger;

public class NamedThreadFactory implements ThreadFactory {
    private final String poolName;
    private final AtomicInteger threadNumber = new AtomicInteger(1);

    public NamedThreadFactory(String poolName) {
        this.poolName = poolName;
    }

    @Override
    public Thread newThread(Runnable r) {
        return new Thread(r, poolName + "-Thread"+threadNumber.getAndIncrement());
    }

    public static void main(String[] args) {
        // 创建自定义的ThreadFactory
        ThreadFactory namedThreadFactory = new NamedThreadFactory("tjc");

        // 创建线程池并设置ThreadFactory
        ExecutorService executorService = Executors.newFixedThreadPool(5, namedThreadFactory);

        // 使用线程池执行任务
        executorService.execute(() -> {
            while (true) {
                System.out.println(Thread.currentThread().getName());
            }
        });

        // 关闭线程池
        executorService.shutdown();
    }
}
